package projet.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.filechooser.*;

import projet.metier.Location;

import projet.metier.GraphGenerator;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.BoxLayout;

public class MainWindows extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	/**
	 * Btn who opened the windows who for load .net files.
	 */
	private JButton btnLoadFile;
	
	/**
	 * Contains all graphics.
	 */
	private JTabbedPane tabbedPane;
	

	public MainWindows(){
		defineWindowAttributes();
		buildWindows();
		
		this.setVisible(true);
	}
	
	/**
	 * Defined all windows's attributes.
	 */
	public void defineWindowAttributes(){
		// On définit le titre de la fenêtre.
		this.setTitle("[A3] Projet Algorithmique !");
		
        this.setLocationRelativeTo(null);
        
        // On définit une taille de base. (La fenêtre sera automatiquement agrandie par la suite)
        this.setSize(1093, 797);
        this.setMinimumSize(new Dimension(910, 750));
        this.setPreferredSize(new Dimension(910, 750));
        
        // Le bouton fermer quitte le processus.
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // On défini la position de la fenêtre si elle est rétrécie. Au centre de l'écran, à peu près.
        this.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/3, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/6);
	}
	
	/**
	 *  Build the windows, create elements to display.
	 */
	public void buildWindows(){
	    getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
	    
	    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	    getContentPane().add(tabbedPane);
	    btnLoadFile = new JButton("Charger fichier .net");
	    btnLoadFile.addActionListener(this);
	    getContentPane().add(btnLoadFile);
	    
	    updateTabbedPane();
	}

	/**
	 * Updates the tabs to load the latest graphs
	 */
	public void updateTabbedPane(){
   
	    tabbedPane.removeAll();
	    
	    File f = new File(GraphGenerator.URL);
	    
	    File[] files = f.listFiles();
	    
	    for (File file : files){
	    	if (file.getName().endsWith(".png")) {
	    		JLabel lblNewLabel = new JLabel("");
	    		JScrollPane scrollPane = new JScrollPane();
	    	    tabbedPane.addTab(file.getName(), null, scrollPane, null);
	    	    scrollPane.setViewportView(lblNewLabel);
	    	    
			    ImageIcon icon;
				try {
					icon = new ImageIcon(ImageIO.read(file));
					lblNewLabel.setIcon(icon);
				    lblNewLabel.repaint();
				} catch (IOException e) {
					e.printStackTrace();
				}
	    	}
	    }
	    f = null;
	    tabbedPane.repaint();
	}
	
	/**
	 * Event sniffer for clics on btn.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if(source == btnLoadFile){
			// Btn load file .net
			JFileChooser fileChooser = new JFileChooser();
			
			// Filter for use only .net files.
	        FileFilter filter = new FileNameExtensionFilter("Fichiers .net", "net");
	        
	        // Add a file filter possibility.
	        fileChooser.addChoosableFileFilter(filter);
	        // Define default filefilter.
	        fileChooser.setFileFilter(filter);
	        // Define only file can be selected.
	        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);// Only file can be loaded.
	        fileChooser.setToolTipText("Chargez le fichier .net.");
	        fileChooser.setDialogTitle("Parcourir");
	        // Disable the choice "All files" for the file filter.
	        fileChooser.setAcceptAllFileFilterUsed(false);
	        
	        // Store the user's choice.
	        int returnValue = fileChooser.showOpenDialog(null);
	        
	        // If the choice is good. (.net file)
	        if (returnValue == JFileChooser.APPROVE_OPTION) {
	        	// Store the selected file.
				File selectedFile = fileChooser.getSelectedFile();

				Location oLocation = new Location();
				oLocation.GenerateGraphicsWithLocations(selectedFile);
				
				JOptionPane.showMessageDialog(null, "Les graphes ont été générés.", "Terminé", JOptionPane.INFORMATION_MESSAGE);
				updateTabbedPane();
	        }
		}
	}
}
